<?php
/*
 **************************************************************************************************
 *	Extends Walker_Nav_Menu - wp_nav_menu()
 **************************************************************************************************
*/
class Prototype_walker_nav_menu extends Walker_Nav_Menu {
/*
 *-------------------------------------------------------------------------------------------------
 *	Displaying an element in our tree
 *-------------------------------------------------------------------------------------------------
*/
	function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {

		$id_field = $this->db_fields['id'];

		// check if a nav_menu has children
		if ( is_object( $args[0] ) )
			$args[0]->has_children = ! empty( $children_elements[$element->$id_field] );

		return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );

    }

/*
 *-------------------------------------------------------------------------------------------------
 *	Add classes to <ul> sub-menus
 *-------------------------------------------------------------------------------------------------
*/
	function start_lvl( &$output, $depth ) {

		// code indent
		$indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' );

		// add custom classes ( classes name depend on your css theme )
		// this theme use class name 'dropdown-menu', 'sub-menu-level1', 'sub-menu-level2', and so on
		$depth_classes = array(
			'dropdown-menu',
			( $depth > 0 ? 'sub-menu-level-' . $depth : '' ),
		);
		$class_names = implode( ' ', $depth_classes );

		// build html
		$output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";

	}

/*
 *-------------------------------------------------------------------------------------------------
 *	Add main / sub classes to <li> and links
 *-------------------------------------------------------------------------------------------------
*/
	function start_el( &$output, $item, $depth, $args ) {

		// code indent
		$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' );

		// add custom classes ( classes name depend on your css theme )
		$depth_classes = array(
			( $depth == 0 ? 'main-menu-item' : 'sub-menu-item' ),
			( $depth >=2 ? 'sub-sub-menu-item' : '' ),
			( $depth % 2 ? 'menu-item-odd' : 'menu-item-even' ),
			'menu-item-depth-' . $depth
		);
		$depth_class_names = esc_attr( implode( ' ', $depth_classes ) );

		// check if a nav_menu has children
		$icon = '';
		if ( $args->has_children ) {
            $item->classes[] = 'dropdown'; // classes name depend on your css theme
			if ( $depth == 0 )
				$icon = ' <i class="icon-angle-down"></i>'; // top level icon
			else
				$icon = ' <i class="icon-angle-right"></i>';
        }

		// passed classes
		$classes = empty( $item->classes ) ? array() : (array) $item->classes;
		$class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );

		// build html
		$output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';

		// link attributes
		$attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
		$attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
		$attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
		$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
		$attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';

		$item_output  = $args->before;
		$item_output .= '<a'. $attributes .'>';
		$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
		$item_output .= $icon . '</a>';
		$item_output .= $args->after;

		// build html
		$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

	}

} // end class Prototype_walker_nav_menu

/*
 **************************************************************************************************
 *	Add a class to a menu item if it has submenus (if not used walker class)
 **************************************************************************************************
*/
function prototype_add_menu_parent_class( $items ) {

	$parents = array();
	// find parent item
	foreach ( $items as $item ) {
		if ( $item->menu_item_parent && $item->menu_item_parent > 0 )
			$parents[] = $item->menu_item_parent;
	}

	foreach ( $items as $item ) {
		if ( in_array( $item->ID, $parents ) )
			// add a parent class for menu item
			$item->classes[] = 'dropdown'; // classes name depend on your css theme
	}

	return $items;

}

//add_filter( 'wp_nav_menu_objects', 'prototype_add_menu_parent_class' 10, 1 );

/*
 **************************************************************************************************
 * 	Extends Walker_page - wp_list_pages()
 **************************************************************************************************
*/
class Prototype_walker_list_pages extends Walker_page {
/*
 *-------------------------------------------------------------------------------------------------
 *	Add classes to <ul> sub-menus
 *-------------------------------------------------------------------------------------------------
*/
	function start_lvl( &$output, $depth ) {

		// code indent
		$indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' );

		// add custom classes ( classes name depend on your css theme )
		// this theme use class name 'dropdown-menu', 'sub-menu-level1', 'sub-menu-level2', and so on
		$depth_classes = array(
			'dropdown-menu',
			( $depth > 0 ? 'sub-menu-level-' . $depth : '' ),
		);
		$class_names = implode( ' ', $depth_classes );

		// build html
		$output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";

	}

/*
 *-------------------------------------------------------------------------------------------------
 *	Add main / sub classes to <li> and links
 *-------------------------------------------------------------------------------------------------
*/
	function start_el( &$output, $page, $depth, $args, $current_page ) {

		// code indent
		$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' );

		extract( $args, EXTR_SKIP );

		// check if a page has children
		$children = get_pages( 'child_of=' . $page->ID );
		if ( count( $children ) > 0 ) {
			if ( $depth == 0 )
				$link_after = ' <i class="icon-angle-down"></i>'; // top level icon
			else
				$link_after = ' <i class="icon-angle-right"></i>';
		}

		// build html
		$output .= $indent . '<li class="dropdown">';
		$output .= '<a href="' . get_page_link( $page->ID );
		$output .= '" title="' . esc_attr( wp_strip_all_tags( apply_filters( 'the_title', $page->post_title, $page->ID ) ) ) . '">';
		$output .= $args->link_before;
		$output .= apply_filters( 'the_title', $page->post_title, $page->ID );
		$output .= $args->link_before . ' ' . $link_after;
		$output .= '</a>';

	}

} // end class Prototype_walker_nav_menu

/*
 **************************************************************************************************
 *	Add a class to a page item if it has submenus (if not used walker class)
 **************************************************************************************************
*/
function prototype_add_page_parent_class( $classes, $page, $depth, $args ) {

    if ( ! empty( $args['has_children'] ) )
        $classes[] = 'drowdown'; // classes name depend on your css theme

	return $classes;

}

//add_filter( 'page_css_class', 'prototype_add_page_parent_class', 10, 4 );

/*
 **************************************************************************************************
 *	Display HTML with meta information for current post, categories, tags, permalink, author, and date
 **************************************************************************************************
*/
if ( ! function_exists( 'prototype_entry_meta' ) ) :

	function prototype_entry_meta() {

		// sticky post
		if ( is_sticky() && is_home() && ! is_paged() )
			echo '<span class="featured-post">' . __( 'Sticky&nbsp;&nbsp;', TEXTDOMAIN ) . '</span>';

		// post date
		if ( ! has_post_format( 'link' ) && 'post' == get_post_type() )
			prototype_entry_date();

		// categories list
		$categories_list = get_the_category_list( __( ', ', TEXTDOMAIN ) );
		if ( $categories_list )
			echo '<li><i class="icon-folder-open"></i> ' . $categories_list . '</li>';

		// tag list
		$tag_list = get_the_tag_list( '', __( ', ', TEXTDOMAIN ) );
		if ( $tag_list )
			echo '<li><i class="icon-tags"></i> ' . $tag_list . '</li>';

		// post author
		if ( 'post' == get_post_type() ) {
			printf( '<li><i class="icon-user"></i> <a href="%1$s" title="%2$s">%3$s</a></li>',
				esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
				esc_attr( sprintf( __( 'View all posts by %s', TEXTDOMAIN ), get_the_author() ) ),
				get_the_author()
			);
		}

	}

endif;

/*
 **************************************************************************************************
 *	Display HTML with date information for current post
 **************************************************************************************************
*/
if ( ! function_exists( 'prototype_entry_date' ) ) :

	function prototype_entry_date( $echo = true ) {

		if ( has_post_format( array( 'chat', 'status' ) ) )
			$format_prefix = __( '%1$s on %2$s', TEXTDOMAIN );
		else
			$format_prefix = '%2$s';

		// date format
		$date = sprintf( '<li><i class="icon-time"></i> <a href="%1$s" title="%2$s"><time class="entry-date" datetime="%3$s">%4$s</time></a></li>',
			esc_url( get_permalink() ),
			esc_attr( sprintf( __( 'Permalink to %s', TEXTDOMAIN ), the_title_attribute( 'echo=0' ) ) ),
			esc_attr( get_the_date() ),
			esc_html( sprintf( $format_prefix, get_post_format_string( get_post_format() ), get_the_date() ) )
		);

		if ( $echo )
			echo $date;

		return $date;

	}

endif;

/*
 **************************************************************************************************
 *	Display navigation to next / previous set of posts
 **************************************************************************************************
*/
if ( ! function_exists( 'prototype_paging_nav' ) ) :

	function prototype_paging_nav() {

		global $wp_query;
		// do not print empty markup if there only one page
		if ( $wp_query->max_num_pages < 2 )
			return;
		?>
		<nav class="navigation paging-navigation" role="navigation">
			<div class="nav-links">
				<?php if ( get_next_posts_link() ) : ?>
					<div class="nav-previous">
						<?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older Posts', TEXTDOMAIN ) ); ?>
					</div>
				<?php endif; ?>
				<?php if ( get_previous_posts_link() ) : ?>
					<div class="nav-next">
						<?php previous_posts_link( __( 'Newer Posts<span class="meta-nav">&rarr;</span>', TEXTDOMAIN ) ); ?>
					</div>
				<?php endif; ?>
			</div>
		</nav>
		<?php
	}

endif;

/*
 **************************************************************************************************
 *	Displays navigation to next / previous post
 **************************************************************************************************
*/
if ( ! function_exists( 'prototype_post_nav' ) ) :

	function prototype_post_nav() {

		global $post;
		// do not print empty markup if there nowhere to navigate
		$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
		$next = get_adjacent_post( false, '', false );

		if ( ! $next && ! $previous )
			return;
		?>
		<nav class="navigation post-navigation" role="navigation">
			<div class="nav-links">
				<?php previous_post_link( '%link', _x( '<span class="meta-nav">&larr;</span> %title', 'Previous post link', TEXTDOMAIN ) ); ?>
				<?php next_post_link( '%link', _x( '%title <span class="meta-nav">&rarr;</span>', 'Next post link', TEXTDOMAIN ) ); ?>
			</div>
		</nav>
		<?php
	}

endif;

/*
 **************************************************************************************************
 *	Comments template callback ( call by wp_list_comments() )
 **************************************************************************************************
*/
if ( ! function_exists( 'prototype_comments_callback' ) ) :

	function prototype_comments_callback( $comment, $args, $depth ) {

		$GLOBALS['comment'] = $comment;

		switch ( $comment->comment_type ) :
			case 'pingback' :
			case 'trackback' :
			?>
				<div <?php comment_class( 'media' ); ?> id="comment-<?php comment_ID(); ?>">
					<div id="comment-<?php comment_ID(); ?>" class="comment media-body">
						<div class="media-content">
							<!-- edit comment link -->
							<div class="edit-comment-link">
								<?php edit_comment_link( __( 'Edit', TEXTDOMAIN ), '<i class="icon-pencil"></i> ', '' ); ?>
							</div>
							<p class="comment-content"><?php _e( 'Pingback : ', TEXTDOMAIN ); ?><?php comment_author_link(); ?></p>
						</div>
					</div>
				</div>
			<?php
				break;

			default :
			?>
				<div <?php comment_class( 'media' ); ?> id="comment-<?php comment_ID(); ?>">
					<!-- avatar -->
					<a class="pull-left"><?php echo get_avatar( $comment, 50 ); ?></a>
					<div id="comment-<?php comment_ID(); ?>" class="comment media-body">
						<div class="media-content">
							<!-- edit comment link -->
							<div class="edit-comment-link">
								<?php edit_comment_link( __( 'Edit', TEXTDOMAIN ), '<i class="icon-pencil"></i> ', '' ); ?>
							</div>
							<!-- comment author, comment date, comment time -->
							<?php
								printf( __( '%1$s %2$s %3$s', TEXTDOMAIN ),
									sprintf( '%s', get_comment_author_link() ),
									'<span class="color">' . __( 'on', TEXTDOMAIN ) . '</span>',
									sprintf( '<a href="%1$s"><time datetime="%2$s">%3$s</time></a>',
										esc_url( get_comment_link( $comment->comment_ID ) ),
										get_comment_time(),
										sprintf( __( '%1$s at %2$s', TEXTDOMAIN ), get_comment_date(), get_comment_time() )
									)
								);
							?>
							<!-- check comment approved -->
							<?php if ( $comment->comment_approved == '0' ) : ?>
								<em class="comment-awaiting-moderation"><?php _e( ' ( Your comment is awaiting moderation )', TEXTDOMAIN ); ?></em>
							<?php endif; ?>
							<!-- comment -->
							<p class="comment-content"><?php comment_text(); ?></p>
							<!-- comment reply link -->
							<div class="reply">
								<?php comment_reply_link( array_merge( $args, array( 'reply_text' => __( 'Reply &darr;', TEXTDOMAIN ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
							</div>
						</div>
					</div>
				</div>
		<?php
				break;
		endswitch;
	}

endif;

/*
 **************************************************************************************************
 *	Breadcrumbs ( http://dimox.net/wordpress-breadcrumbs-without-a-plugin )
 **************************************************************************************************
*/
if ( ! function_exists( 'prototype_dimox_breadcrumbs' ) ) :

	function prototype_dimox_breadcrumbs() {

		/* options */
		$text[ 'home' ] = 'Home'; // text for the 'Home' link
		$text[ 'category' ] = 'Archive by Category "%s"'; // text for a category page
		$text[ 'search' ] = 'Search Results for "%s" Query'; // text for a search results page
		$text[ 'tag' ] = 'Posts Tagged "%s"'; // text for a tag page
		$text[ 'author' ] = 'Articles Posted by %s'; // text for an author page
		$text[ '404' ] = 'Error 404'; // text for the 404 page

		$show_current = 1; // 1 - show current post/page/category title in breadcrumbs, 0 - don't show
		$show_on_home = 1; // 1 - show breadcrumbs on the homepage, 0 - don't show
		$show_home_link = 1; // 1 - show the 'Home' link, 0 - don't show
		$show_title = 1; // 1 - show the title for the links, 0 - don't show
		$delimiter = ' &raquo; '; // delimiter between crumbs
		$before = '<span class="current">'; // tag before the current crumb
		$after = '</span>'; // tag after the current crumb
		/* end of options */

		global $post;
		$home_link = home_url( '/' );
		$link_before = '<span typeof="v:Breadcrumb">';
		$link_after = '</span>';
		$link_attr = ' rel="v:url" property="v:title"';
		$link = $link_before . '<a' . $link_attr . ' href="%1$s">%2$s</a>' . $link_after;
		$parent_id = $parent_id_2 = $post->post_parent;
		$frontpage_id = get_option( 'page_on_front' );

		if ( is_home() || is_front_page() ) {
			if ( $show_on_home == 1 )
				echo '<div class="breadcrumbs"><a href="' . $home_link . '">' . $text[ 'home' ] . '</a></div>';
		} else {
			echo '<div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';
			if ( $show_home_link == 1 ) {
				echo '<a href="' . $home_link . '" rel="v:url" property="v:title">' . $text[ 'home' ] . '</a>';
				if ( $frontpage_id == 0 || $parent_id != $frontpage_id )
					echo $delimiter;
			}
			if ( is_category() ) {
				$this_cat = get_category( get_query_var( 'cat' ), false );
				if ( $this_cat->parent != 0 ) {
					$cats = get_category_parents( $this_cat->parent, TRUE, $delimiter );
					if ( $show_current == 0 )
						$cats = preg_replace( "#^(.+)$delimiter$#", "$1", $cats );
					$cats = str_replace( '<a', $link_before . '<a' . $link_attr, $cats );
					$cats = str_replace( '</a>', '</a>' . $link_after, $cats );
					if ( $show_title == 0 )
						$cats = preg_replace( '/ title="(.*?)"/', '', $cats );
					echo $cats;
				}
				if ( $show_current == 1 )
					echo $before . sprintf( $text[ 'category' ], single_cat_title( '', false ) ) . $after;
			} elseif ( is_search() ) {
				echo $before . sprintf( $text[ 'search' ], get_search_query() ) . $after;
			} elseif ( is_day() ) {
				echo sprintf( $link, get_year_link( get_the_time( 'Y' ) ), get_the_time( 'Y' ) ) . $delimiter;
				echo sprintf( $link, get_month_link( get_the_time( 'Y' ), get_the_time( 'm' ) ), get_the_time( 'F' ) ) . $delimiter;
				echo $before . get_the_time( 'd' ) . $after;
			} elseif ( is_month() ) {
				echo sprintf( $link, get_year_link( get_the_time( 'Y' ) ), get_the_time( 'Y' ) ) . $delimiter;
				echo $before . get_the_time( 'F' ) . $after;
			} elseif ( is_year() ) {
				echo $before . get_the_time( 'Y' ) . $after;
			} elseif ( is_single() && !is_attachment() ) {
				if ( get_post_type() != 'post' ) {
					$post_type = get_post_type_object( get_post_type() );
					$slug = $post_type->rewrite;
					printf( $link, $home_link . '/' . $slug[ 'slug' ] . '/', $post_type->labels->singular_name );
					if ( $show_current == 1 )
						echo $delimiter . $before . get_the_title() . $after;
				} else {
					$cat = get_the_category();
					$cat = $cat[ 0 ];
					$cats = get_category_parents( $cat, TRUE, $delimiter );
					if ( $show_current == 0 )
						$cats = preg_replace( "#^(.+)$delimiter$#", "$1", $cats );
					$cats = str_replace( '<a', $link_before . '<a' . $link_attr, $cats );
					$cats = str_replace( '</a>', '</a>' . $link_after, $cats );
					if ( $show_title == 0 )
						$cats = preg_replace( '/ title="(.*?)"/', '', $cats );
					echo $cats;
					if ( $show_current == 1 )
						echo $before . get_the_title() . $after;
				}
			} elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
				$post_type = get_post_type_object( get_post_type() );
				echo $before . $post_type->labels->singular_name . $after;
			} elseif ( is_attachment() ) {
				$parent = get_post( $parent_id );
				$cat = get_the_category( $parent->ID );
				$cat = $cat[ 0 ];
				$cats = get_category_parents( $cat, TRUE, $delimiter );
				$cats = str_replace( '<a', $link_before . '<a' . $link_attr, $cats );
				$cats = str_replace( '</a>', '</a>' . $link_after, $cats );
				if ( $show_title == 0 )
					$cats = preg_replace( '/ title="(.*?)"/', '', $cats );
				echo $cats;
				printf( $link, get_permalink( $parent ), $parent->post_title );
				if ( $show_current == 1 )
					echo $delimiter . $before . get_the_title() . $after;
			} elseif ( is_page() && !$parent_id ) {
				if ( $show_current == 1 )
					echo $before . get_the_title() . $after;
			} elseif ( is_page() && $parent_id ) {
				if ( $parent_id != $frontpage_id ) {
					$breadcrumbs = array( );
					while ( $parent_id ) {
						$page = get_page( $parent_id );
						if ( $parent_id != $frontpage_id ) {
							$breadcrumbs[ ] = sprintf( $link, get_permalink( $page->ID ), get_the_title( $page->ID ) );
						}
						$parent_id = $page->post_parent;
					}
					$breadcrumbs = array_reverse( $breadcrumbs );
					for ( $i = 0; $i < count( $breadcrumbs ); $i++ ) {
						echo $breadcrumbs[ $i ];
						if ( $i != count( $breadcrumbs ) - 1 )
							echo $delimiter;
					}
				}
				if ( $show_current == 1 ) {
					if ( $show_home_link == 1 || ($parent_id_2 != 0 && $parent_id_2 != $frontpage_id) )
						echo $delimiter;
					echo $before . get_the_title() . $after;
				}
			} elseif ( is_tag() ) {
				echo $before . sprintf( $text[ 'tag' ], single_tag_title( '', false ) ) . $after;
			} elseif ( is_author() ) {
				global $author;
				$userdata = get_userdata( $author );
				echo $before . sprintf( $text[ 'author' ], $userdata->display_name ) . $after;
			} elseif ( is_404() ) {
				echo $before . $text[ '404' ] . $after;
			}
			if ( get_query_var( 'paged' ) ) {
				if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
					echo ' (';
				echo __( 'Page' ) . ' ' . get_query_var( 'paged' );
				if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
					echo ')';
			}
			echo '</div><!-- .breadcrumbs -->';
		}
	}

endif;